************************************************************************
* 				RESPONDENTS PLOTS AND MULTILEVEL DIAGNOSTICS		   *
************************************************************************

use "data/iraqmerged.dta", clear

//Respondents plots 

***FIGURE 4***
twoway ///
bar rspdts1 datevar if time==1, base(0) barw(0.85) || ///
bar rspdts1 datevar if time==2, base(0) barw(0.85) || ///
bar rspdts1 datevar if time==3, base(0) barw(0.85) ///
legend(off) ///
ylabel(0(40)160, nogrid) ///
xlabel(19871 "28 May" 19876 "02 Jun" 19881 "07 Jun" 19886 "12 Jun" 19891 "17 Jun" 19896 "22 Jun", nogrid) ///
ytitle(# Respondents) xtitle(Date) xline(19884, lpatt(dash) lcol(black)) ///
text(140 19886 "Fall of Mosul", size(small)) scheme(tufte) ///
note(Note: Respondents in Kurdish regions removed, size(vsmall)) 
graph export "plots/respdts.png", replace

***FIGURE 6***

*NOTE: to use svyplot copy svyplot.ado into relevant "Stata/base/ado path"
*taken from: https://gist.github.com/briatte/5099538
recode treat(1=1 "After Mosul")(0=0 "Before Mosul"),into (treatlab)

svyplot ident treatlab sect  if kurd==0, asc hor stack scheme(lean2) ///
title("") legend(pos(6)) note(Note: Respondents in Kurdish regions removed, size(vsmall))

gr_edit plotregion1.bars[16].style.editstyle linestyle(width(none)) editcopy
gr_edit plotregion1.bars[17].style.editstyle linestyle(width(none)) editcopy
gr_edit plotregion1.bars[18].style.editstyle linestyle(width(none)) editcopy
gr_edit plotregion1.bars[19].style.editstyle linestyle(width(none)) editcopy
gr_edit plotregion1.bars[20].style.editstyle linestyle(width(none)) editcopy
gr_edit plotregion1.bars[16].style.editstyle shadestyle(color(gs0)) editcopy
gr_edit plotregion1.bars[16].style.editstyle linestyle(color(gs0)) editcopy
gr_edit plotregion1.bars[17].style.editstyle shadestyle(color(gs5)) editcopy
gr_edit plotregion1.bars[17].style.editstyle linestyle(color(gs5)) editcopy
gr_edit plotregion1.bars[18].style.editstyle shadestyle(color(gs8)) editcopy
gr_edit plotregion1.bars[18].style.editstyle linestyle(color(gs8)) editcopy
gr_edit plotregion1.bars[19].style.editstyle shadestyle(color(gs11)) editcopy
gr_edit plotregion1.bars[19].style.editstyle linestyle(color(gs11)) editcopy
gr_edit plotregion1.bars[20].style.editstyle shadestyle(color(gs13)) editcopy
gr_edit plotregion1.bars[20].style.editstyle linestyle(color(gs13)) editcopy
gr_edit plotregion1.barlabels[16].style.editstyle color(white) editcopy

graph export "plots/svygph.png", replace


//Multilevel model diagnostics

*rankings for natident caterpillar
xtmelogit natident if kurd==0 || disid:, variance
*drop pickone_dis
egen pickone_dis = tag(disid)
predict u0, reffects
predict u0se, reses
egen u0rank = rank(u0) if pickone_dis==1
sort u0rank
list disid u0 u0se u0rank if pickone_dis==1
gen dislab = u0 + 1.96*u0se + 0.05
sort u0rank
list disid u0 u0se u0rank if pickone_dis==1
*rankings for arabident caterpillar
xtmelogit arabident if kurd==0 || disid:, variance
egen pickone_disar = tag(disid)
predict u0ar, reffects
predict u0sear, reses
egen u0rankar = rank(u0ar) if pickone_disar==1
sort u0rankar
list disid u0ar u0sear u0rankar if pickone_disar==1
//Tied rankings--force separate rank
replace u0rankar=4 if u0rankar==4.5&disid==1
replace u0rankar=5 if u0rankar==4.5&disid==26
replace u0rankar=12 if u0rankar==12.5&disid==35
replace u0rankar=13 if u0rankar==12.5&disid==18
gen dislabar = u0ar + 1.96*u0sear + 0.05
sort u0rankar
list disid u0ar u0sear u0rankar if pickone_disar==1
*rankings for islident caterpillar
xtmelogit islident if kurd==0 || disid:, variance
egen pickone_disis = tag(disid)
predict u0is, reffects
predict u0seis, reses
egen u0rankis = rank(u0is) if pickone_disis==1
sort u0rankis
list disid u0is u0seis u0rankis if pickone_disis==1
gen dislabis = u0is + 1.96*u0seis + 0.05
sort u0rankis
list disid u0is u0seis u0rankis if pickone_disis==1


***FIGURE B.1***
serrbar u0 u0se u0rank if pickone_dis==1, scale(1.96) yline(0) ///
addplot(scatter dislab u0rank, msymbol(none) mlabel(distesoc) ///
mlabposition(1) mlabangle(vertical) mlabsize(tiny)) aspectratio(1) ///
ylab(,nogrid) ///
xtitle("Rank") ytitle("District residuals") legend(off) title("National identification", size(vsmall)) scheme(tufte)
graph export "plots/serrnat.pdf", replace

serrbar u0ar u0sear u0rankar if pickone_disar==1 , scale(1.96) yline(0) ///
addplot(scatter dislabar u0rankar, msymbol(none) mlabel(distesoc) ///
mlabposition(1) mlabangle(vertical) mlabsize(tiny)) aspectratio(1) ///
ylab(,nogrid) ///
xtitle("Rank") ytitle("District residuals") legend(off) title("Arab identification", size(vsmall)) scheme(tufte)
graph export "plots/serrar.pdf", replace

serrbar u0is u0seis u0rankis if pickone_disis==1, scale(1.96) yline(0) ///
addplot(scatter dislabis u0rankis, msymbol(none) mlabel(distesoc) ///
mlabposition(1) mlabangle(vertical) mlabsize(tiny)) aspectratio(1) ///
ylab(,nogrid) ///
xtitle("Rank") ytitle("District residuals") legend(off) title("Islamic identification", size(vsmall)) scheme(tufte)
graph export "plots/serris.pdf", replace
